<template>
  <!-- 一条文章单元格 -->
  <!-- 点击文章跳转到文章详情列表 携带参数?传参 art_id是自定义 后面的id需要动态获取 -->
  <van-cell @click="$router.push(`/article?art_id=${arrart.art_id}`)">
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{arrart.title}}</span>
         <!-- 单图 -->
          <img
          v-if='arrart.cover.type === 1'
          class="thumb"
          :src="arrart.cover.images"
          />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box"  v-if='arrart.cover.type > 1 '>
        <img
        v-for="(imgURL,idx) in arrart.cover.images" :key="idx"
        class="thumb"
        :src="imgURL"  />
     </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>作者:{{arrart.aut_name}}</span>
          <span>{{arrart.comm_count}}评论</span>
          <span>发布时间:{{$timeAgo(arrart.pubdate)}}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="onCloseClick" v-if="showCorse" />
        <van-action-sheet v-model="show" :actions="actions" @select="onSelect" get-container="body" :cancel-text="cancelText" @cancel="onCancel" />
        <!-- 反馈面板  -->
        <van-dialog
        v-jujiao
        v-model="isShow"
        show-cancel-button
        get-container="body"
        :beforeClose='onBeforeCloseFn'
        >
            <van-field v-model.trim="msg" type='textarea' placeholder="请输入反馈内容"></van-field>
        </van-dialog>
      </div>
    </template>
  </van-cell>
</template>

<script>
import { firstActions, secondActions } from '@/api/reports'
import { articleDislikeAPI, articleReportAPI } from '@/api/home'
export default {
  props: {
    // 接受articleList中的对象
    arrart: {
      type: Object,
      required: true
    },
    i: Number,
    // 默认不显示X按钮 父传子接受boolean 来控制显示和隐藏
    showCorse: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // ...省略其他
      actions: firstActions,
      show: false,
      cancelText: '取消', // 底部按钮文字
      isShow: false, // 反馈面板的隐藏
      msg: '' // 输入内容
    }
  },
  methods: {
    async onBeforeCloseFn (action, done) {
      // console.log(action, done)
      if (action === 'cancel') {
        done()
        return
      }
      if (!this.msg) {
        this.$toast.fail('请输入反馈内容！')
        // 不关闭反馈面板
        done(false)
        return
      }
      try {
        // 不需要接受返回数据
        await articleReportAPI({
          artid: this.arrart.art_id,
          type: '0',
          remark: this.msg
        })
        this.$toast.success('反馈成功！')
        this.msg = ''
      } catch (e) {
        this.$toast.fail('反馈出错！')
      } finally {
        done()
      }
    },
    // 反馈面板点击事件
    async onSelect (action) {
      if (action.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (action.name === '不感兴趣') {
        try {
          await articleDislikeAPI({
            artid: this.arrart.art_id
          })
          this.show = false
          this.$emit('del-art', this.i)
          this.$toast.success('操作成功')
        } catch (err) {
          console.log(err)

          this.$toast.fail('操作失败')
        }
      } else if (action.name === '拉黑作者') {

      } else {
        if (action.id === 0) {
          // articleReportAPI
          // console.log(123)

          this.show = false
          this.isShow = true
        } else { // 二级标题正常反馈
          // console.log(2)
          try {
            // 不需要接受返回数据
            await articleReportAPI({
              artid: this.arrart.art_id,
              type: action.id + '',
              remark: action.name
            })
            this.$toast.success('举报成功！')
          } catch (e) {
            this.$toast.fail('举报失败！')
          } finally {
            // 隐藏一级二级标题
            this.show = false
          }
        }
      }
    },
    // 点击了叉号按钮
    onCloseClick () {
      // 展示动作面板
      this.show = true
    },
    // 取消事件
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
  margin: 0 3px;
  &:first-child{
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
